//
//  YGScienceCenterViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/4/11.
//

#import "YGScienceCenterViewController.h"
#import "YGScienceCenterHeaderView.h"
#import "YGHomeBLL.h"
#import "YGSubscribeCollectionViewCell.h"
#import "YGSicenceCollectionReusableView.h"
#import "YGArticleViewController.h"

@interface YGScienceCenterViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) YGScienceCenterHeaderView *loseWeightView;
@property (nonatomic, strong) YGScienceCenterHeaderView *healthDietView;
@property (nonatomic, strong) YGScienceCenterHeaderView *bodyPlanView;          //推荐文章视图

@property (nonatomic, strong) UICollectionView *scienceCenterCollectionView;
@property (nonatomic, strong) NSArray *subscribeArray;      //订阅分类list
@property (nonatomic, strong) NSArray *typeArray;           //分类list
@property (nonatomic, strong) NSMutableArray *erkeArray;       //儿科list
@property (nonatomic, strong) NSMutableArray *neikeArray;       //内科list
@property (nonatomic, strong) NSMutableArray *waikeArray;       //外科list



@end

@implementation YGScienceCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置导航栏属性
    //self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0x37C8D7);
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0x333333)}];
    
    _subscribeArray = [NSMutableArray array];
    _erkeArray = [NSMutableArray array];
    _neikeArray = [NSMutableArray array];
    _waikeArray = [NSMutableArray array];
    
    
    [self initView];
    [self initconstraints];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}
-(void)initView{
    
    [self.view addSubview: self.scienceCenterCollectionView];

}

-(void)initconstraints{
    
    [self.loseWeightView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.scienceCenterCollectionView.mas_top).offset(-290);
        make.left.mas_equalTo(self.bodyPlanView.mas_left);
        make.width.mas_equalTo(174);
        make.height.mas_equalTo(110);
    }];
    
    [self.healthDietView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.loseWeightView.mas_top);
        make.left.mas_equalTo(self.loseWeightView.mas_right).offset(10);
        make.right.mas_equalTo(self.bodyPlanView.mas_right);
        make.width.mas_equalTo(174);
        make.height.mas_equalTo(110);
    }];
    
    [self.bodyPlanView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.healthDietView.mas_bottom).offset(10);
        make.centerX.mas_equalTo(self.scienceCenterCollectionView.mas_centerX);
        make.width.mas_equalTo(355.5);
        make.height.mas_equalTo(160);
       
     
    }];
    
    [self.scienceCenterCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.width.mas_equalTo(self.view);
      
    }];
  
}

static NSString *identify=@"YGSubscribeCollectionViewCell";


#pragma  -请求网络
-(void)requestData{
    WeakSelf(self);
    [YGHomeBLL requestScienceCenterTypeInfoSuccess:^(YGScienceCenterTypeModel * _Nonnull model) {
        //订阅分类数据
        weakself.subscribeArray = model.subscribe;
        
        //分类数据
        weakself.typeArray = model.type;
        YGScienceTypeModel *tpModel = (YGScienceTypeModel *)[model.type firstObject];
        weakself.erkeArray = [tpModel.lowerData copy];
        tpModel = [model.type objectAtIndex:1];
        weakself.neikeArray = [tpModel.lowerData copy];
        tpModel = [model.type objectAtIndex:2];
        weakself.waikeArray = [tpModel.lowerData copy];
        
        
        //推荐文章赋值
        YGScienceCenterArticleModel *articModel = (YGScienceCenterArticleModel *)[model.article firstObject];
        [weakself.loseWeightView configureModel:articModel];
        articModel = [model.article objectAtIndex:1];
        [weakself.healthDietView configureModel:articModel];
        articModel = [model.article objectAtIndex:2];
        [weakself.bodyPlanView configureModel:articModel];

        [weakself.scienceCenterCollectionView reloadData];  //拿到数据刷新列表
        } fail:^(NSError * _Nonnull error) {
            
        }];
}

#pragma event

-(void)clickInfoFoodRecommenAction{
    
}

-(void)clickInfoWeightLossAction{
    
}

-(void)clickInfohealthyDietAction{
    
}

#pragma -delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 4;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        //有数据就有头部，没数据返回0
        if (self.subscribeArray.count > 0) {
            
            return CGSizeMake(self.view.width,20);
        }
        return CGSizeZero;
    }
    return CGSizeMake(self.view.width, 20);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        YGSicenceCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass(YGSicenceCollectionReusableView.class) forIndexPath:indexPath];
            [headerView removeFromSuperview];
                // headerView上添加其他的view和控件
        if (indexPath.section == 0) {
            [headerView setTitle:@"我的订阅"];
        }else if (indexPath.section == 1){
            [headerView setTitle:@"儿科"];
        }else if (indexPath.section == 2){
            [headerView setTitle:@"内科"];
        }else{
            [headerView setTitle:@"外科"];
        }
        return headerView;
        }
    return nil;
}
//点击collectionview触发事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        //订阅
        YGArticleViewController *vc = [[YGArticleViewController alloc]init];
        YGScienceCenterSubscribeArrayModel *model;
        model = [self.subscribeArray objectAtIndex:indexPath.row];
        vc.typeId = model.id;
        vc.name = model.name;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else{
        YGArticleViewController *vc = [[YGArticleViewController alloc] init];
        YGScienceCenterSubscribeArrayModel *model;
        if (indexPath.section == 1) {
            model = [self.erkeArray objectAtIndex:indexPath.row];
        }else if (indexPath.section == 2){
            model = [self.neikeArray objectAtIndex:indexPath.row];
        }else{
            model = [self.waikeArray objectAtIndex:indexPath.row];
        }
        vc.typeId = model.id;
        vc.name = model.name;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

//设置每个UIcollectionview的大小

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(70, 70);
}

//设置collectioncell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    YGSubscribeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    YGScienceCenterSubscribeArrayModel *model ;
    if (indexPath.section == 0) {
      model = [self.subscribeArray objectAtIndex:indexPath.row];
    }else if(indexPath.section == 1){
        model = [self.erkeArray objectAtIndex:indexPath.row];
    }else if(indexPath.section == 2){
        model = [self.neikeArray objectAtIndex:indexPath.row];
    }else if(indexPath.section == 3){
        model = [self.waikeArray objectAtIndex:indexPath.row];
    }
    [cell configurfeModel:model];
    return cell;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return self.subscribeArray.count;
    }else if (section == 1){
        return self.erkeArray.count;
    }else if (section == 2){
        return self.neikeArray.count;
    }else{
        return self.waikeArray.count;
    }
}
    

#pragma setter


- (YGScienceCenterHeaderView *)loseWeightView{
    if (!_loseWeightView) {
        _loseWeightView = [[YGScienceCenterHeaderView alloc]init];

    }return _loseWeightView;
}

- (YGScienceCenterHeaderView *)healthDietView{
    if (!_healthDietView) {
        _healthDietView = [[YGScienceCenterHeaderView alloc]init];

    }
    return _healthDietView;
}

- (YGScienceCenterHeaderView *)bodyPlanView{
    if (!_bodyPlanView) {
        _bodyPlanView = [[YGScienceCenterHeaderView alloc]init];
    }
    return _bodyPlanView;
}

- (UICollectionView *)scienceCenterCollectionView{
    if (!_scienceCenterCollectionView) {
        //创建流布局对象
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
        
        //设置每个单元格的大小
    //    flowlayout.itemSize = CGSizeMake(70, 70);
        
        //设置滚动方向（垂直/水平）
        [flowlayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        
        //设置各分区上左下右空白的大小
        flowlayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        
        //设置两行单元格之间的行距
        flowlayout.minimumLineSpacing = 10;
        
        //设置两个单元格之间的间距
        flowlayout.minimumInteritemSpacing = 10;
 
        //设置每个section的headerview大小
        flowlayout.headerReferenceSize = CGSizeMake(self.view.width, 20);
        
        _scienceCenterCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) collectionViewLayout:flowlayout];
        _scienceCenterCollectionView.dataSource = self;
        _scienceCenterCollectionView.delegate = self;
        _scienceCenterCollectionView.backgroundColor = [UIColor whiteColor];
        [_scienceCenterCollectionView registerClass:YGSubscribeCollectionViewCell.class forCellWithReuseIdentifier:identify];
       
        //设置添加推荐文章
        self.scienceCenterCollectionView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
        [self.scienceCenterCollectionView registerClass:YGSicenceCollectionReusableView.class
                             forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                    withReuseIdentifier:NSStringFromClass(YGSicenceCollectionReusableView.class)];
        
        
        [self.scienceCenterCollectionView addSubview:self.loseWeightView];
        [self.scienceCenterCollectionView addSubview:self.healthDietView];
        [self.scienceCenterCollectionView addSubview:self.bodyPlanView];
    
        
    }
    return _scienceCenterCollectionView;
}



@end
